<?php
/**
 * File from WEBO Video Transmuter, WEBO Software (http://www.webogroup.com/)
 * Provides core client interface.
 *
 **/
class webo_video_transmuter {
/* constructor */
	function webo_video_transmuter () {
/* load options */
		$this->basedir = realpath(dirname(__FILE__)) . '/../';
		$this->config = $this->basedir . 'config.wvt.php';
		require($this->config);
		$this->options = $wvt_options;
		$this->rootdir = empty($_SERVER['DOCUMENT_ROOT']) ? '' : realpath($_SERVER['DOCUMENT_ROOT']);
		$this->relativebase = str_replace($this->rootdir, "", $this->basedir);
		$this->videodir = str_replace($this->rootdir, "", $wvt_options['videodir']);
/* define HTML5 support */
		$ua = strtolower(empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT']);
		$br = strpos($ua, 'opera') !== false ? 'op' : (strpos($ua, 'webkit') !== false ? 'wk' : (strpos($ua, 'msie') !== false ? 'ie' : (strpos($ua, 'mozilla') !== false && strpos($ua, 'compatible') === false ? 'fx' : '')));
		$v = 0;
		switch ($br) {
			case 'op';
			case 'wk':
				if (preg_match('/(chrome|version)\/([\d.]+)/i', $ua, $matches)) {
					$v = $matches[2];
				}
				break;
			default:
				if (preg_match('/.+(?:rv|ie)[\/: ]([\d.]+)/', $ua, $matches)) {
					$v = $matches[1];
				}
				break;
		}
		$this->is_ie = $br == 'ie' && $v >= 9.0;
		$this->is_wk = $br == 'wk' && strpos($ua, 'chrome') === false && $v >= 3.1;
		$mobiles = array( 'android', 'blackberry', 'htc', 'iphone', 'ipod', 'ipad', 'lg', 'mot', 'mobile', 'netfront', 'nokia', 'mini', 'palm', 'ppc', 'samsung', 'smartphone', 'sonyericsson', 'symbian', 'up.browser', 'webos');
		$j = 0;
		while (!empty($mobiles[$j]) && strpos($ua, $mobiles[$j++]) === false) {}
		$this->is_mobile = $j != count($mobiles) ? 1 : 0;
		$this->is_html5_compatible = ($br == 'wk' && $v >= 4) || ($br == 'op' && $v >= 10.6) || ($br == 'fx' && $v >= 1.92) || $this->is_ie;
	}
/* HTML code parser/converter */
	function convert ($content) {
		if (strpos($content, '[video')) {
			$video = '';
			if ($this->is_html5_compatible) {
/* add fullscreen button */
				$video .= $this->is_wk || $this->is_mobile ? '' : '<a href="javascript:wvt_switch(###ID###);void(0)" class="wvt_fullscreen" id="wvt_switch_###ID###" style="margin-left:###WIDTH30###px" title="Full Screen / Normal Screen"></a>';
/* add general video tag */
				$video .= '<video ';
/* mp4 for IE */
				if ($this->is_ie) {
					$video .= 'src="' .
						$this->videodir .
						'mp4/###VIDEO###.mp4" ';
				}
				$video .= 'preload="none" width="###WIDTH###" height="###HEIGHT###" id="wvt_player_###ID###" controls="controls" poster="' .
					$this->videodir .
					'thumb/###VIDEO###.jpg"';
				$video .= $this->is_mobile ? ' onclick="this.paused?this.play():this.pause()"' : '';
				$video .= $video .= $this->is_mobile ? '><source src="' .
					$this->videodir .
					'mp4/###VIDEO###.m4v"/>' : '><source src="' .
					$this->videodir .
					'theora/###VIDEO###.ogv" type=\'video/ogg;codecs="theora,vorbis"\'/><source src="' .
					$this->videodir .
					'mp4/###VIDEO###.mp4" type=\'video/mp4;codecs="avc1.4D401E,mp4a.40.2"\'/>';
			}
			$video .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="wvt_player" width="###WIDTH###" height="###HEIGHT###"><param name="movie" value="' .
				$this->relativebase .
				'libs/player/player.swf"/><param name="allowfullscreen" value="true"/><param name="allowscriptaccess" value="always"/><param name="flashvars" value="file=' .
				$this->videodir .
				'mp4/###VIDEO###.mp4&skin=' .
				$this->relativebase .
				'libs/player/skin/wvt.zip&image=' .
				$this->videodir .
				'thumb/###VIDEO###.jpg"/><embed  type="application/x-shockwave-flash" id="wvt_player_embed" name="wvt_player_embed" src="' .
				$this->relativebase .
				'libs/player/player.swf" width="###WIDTH###" height="###HEIGHT###" allowscriptaccess="always" allowfullscreen="true" flashvars="file=' .
				$this->videodir .
				'mp4/###VIDEO###.mp4&skin=' .
				$this->relativebase .
				'libs/player/skin/wvt.zip&image='.
				$this->videodir .
				'thumb/###VIDEO###.jpg"/></object>';
			if ($this->is_html5_compatible) {
				$video .= '</video>';
			}
			preg_match_all("!\[video[^\]]+\]!is", $content, $matches, PREG_SET_ORDER);
			preg_match_all("!<body!is", $content, $body, PREG_SET_ORDER);
			if ($this->is_html5_compatible && !$this->is_wk) {
				$content = preg_replace("!(<body[^>]*>)!is", '$1<script type="text/javascript">var wvt_remember;function wvt_switch(i){var a=document,b=a.getElementById("wvt_switch_"+i),c=b.className,d=a.getElementById("wvt_player_"+i),e=a.body,f=a.getElementById("wvt_layer"),g=a.documentElement.clientWidth||e.clientWidth,h=a.documentElement.clientHeight||e.clientHeight,j=d.width,k=d.height,m=j/k,l=g/h<m;if(c=="wvt_fullscreen"){a.addEventListener("keyup",function(e){if((e||window.event).keyCode==27){wvt_switch(i)}},false);wvt_remember=d.parentNode;f.style.display="block";f.appendChild(d);d.style.zIndex=1;d.style.width=(l?g:h*m-60)+"px";d.style.height=(l?g/m:h-60)+"px";d.style.paddingLeft=d.style.paddingRight=b.style.left=((l?0:g-h*m)/2+30)+"px";d.style.paddingTop=d.style.paddingBottom=((l?(h-g/m)/2:0)+30)+"px";d.style.background="#000";b.style.marginLeft=(l?g:h*m-86)+"px";d.style.position="absolute";d.style.zIndex=2147482999;b.className="wvt_normalscreen"}else{f.style.display="none";d.style.width=d.width+"px";d.style.height=d.height+"px";d.style.position="static";d.style.padding=0;d.style.zIndex=1;b.style.marginLeft=(j-26)+"px";wvt_remember.appendChild(d);b.className="wvt_fullscreen"}}</script><div id="wvt_layer" style="background:#000;display:none;height:100%;left:0;position:fixed;top:0;width:100%;zIndex:2147483000"></div>', $content);
				$content = preg_replace("!(<head[^>]*>)!is", '$1<style type="text/css">.wvt_fullscreen{background:url();display:block;float:right;height:29px;outline:none;width:26px;z-index:1}.wvt_normalscreen{background:url();display:block;height:29px;outline:none;position:fixed;top:0;width:26px;z-index:2147483000}</style>', $content);
			}
			$body_pos = strpos($content, $body[0][0]);
			$replaces_from = array();
			$replaces_to = array();
			$i = 0;
			foreach ($matches as $match) {
				$str = $match[0];
				if (($pos = strpos($content, $str)) > $body_pos) {
					$i++;
					$replaces_from[] = $str;
					$width = preg_replace("!.*(width=([0-9]+)).*!is", "$2", $str);
					$width30 = $width - 26;
					$height = preg_replace("!.*(height=([0-9]+)).*!is", "$2", $str);
					$file = str_replace('"', '', preg_replace("!.*(src=(\"[^\t\n\r]+\")).*!is", "$2", $str));
					$description = preg_replace("!.*(desc=(\"[^\t\n\r]+)\").*!is", "$2", $str);
					$description = $description == $str ? '' : str_replace('"', '', $description);
					$replaces_to[] = str_replace(array('###WIDTH###', '###WIDTH30###', '###HEIGHT###', '###DESC###', '###VIDEO###', '###ID###'), array($width, $width30, $height, $description, $file, $i), $video);
				} else {
					$content = substr_replace($content, '', $pos, strlen($str));
				}
			}
			if (count($replaces_from)) {
				$content = str_replace($replaces_from, $replaces_to, $content);
			}
		}
		return $content;
	}
}